package com.timoon.sample.web.form;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import com.googlecode.wicket.jquery.ui.form.autocomplete.AutoCompleteTextField;
import com.googlecode.wicket.jquery.ui.form.button.AjaxButton;
import com.googlecode.wicket.jquery.ui.form.palette.Palette;
import com.googlecode.wicket.jquery.ui.form.slider.Slider;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;
import com.timoon.sample.dao.model.TUser;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class Examples extends Panel{

	public Examples(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		final JQueryFeedbackPanel feedback=new JQueryFeedbackPanel("wk_feedback");
		add(feedback);
		
		AutoCompleteTextField<String> autoComplete=new AutoCompleteTextField<String>("wk_autoComplete",new Model<String>("")) {
			
			@Override
			protected List<String> getChoices(String arg0) {
				// TODO Auto-generated method stub
				System.err.println(arg0);
				List<String> list=new ArrayList<String>();
				for(int i=0;i<5;i++){
					list.add(arg0+i);
				}
				return list;
			}
			
			@Override
			protected void onSelected(AjaxRequestTarget target) {
				// TODO Auto-generated method stub
				super.onSelected(target);
				this.info("您选择的是:"+this.getDefaultModel().getObject());
				target.add(feedback);
			}
		};
		add(autoComplete);
		
		final AjaxCheckBox checkbox=new AjaxCheckBox("wk_checkbox", new Model<Boolean>(true)){
			@Override
			protected void onSelectionChanged(Boolean newSelection) {
				// TODO Auto-generated method stub
				super.onSelectionChanged(newSelection);
				
			}

			@Override
			protected void onUpdate(AjaxRequestTarget arg0) {
				// TODO Auto-generated method stub
				System.err.println(this.getModelObject());
			}
		};
		add(checkbox);
		AjaxLink checkLink=new AjaxLink("wk_check_link") {

			@Override
			public void onClick(AjaxRequestTarget arg0) {
				// TODO Auto-generated method stub
				this.info("您的选择是："+checkbox.getDefaultModel().getObject());
				arg0.add(feedback);
			}
		};
		add(checkLink);
		
		
		//数据交换
		Form form=new Form("wk_form");
		add(form);
		
		List<TUser> users=new ArrayList<TUser>();
		for(int i=0;i<10;i++){
			TUser user=new TUser();
			user.setName("name-"+i);
			users.add(user);
		}
		
		IChoiceRenderer<String> render=new ChoiceRenderer<String>("name","name");
		final Palette palette=new Palette<>("wk_palette",new Model(new ArrayList()), new Model((Serializable) users), render, 10, true);
		form.add(palette);
		
		
		form.add(new AjaxButton("wk_save"){
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				Collection selected=palette.getModelCollection();
				StringBuffer names=new StringBuffer();
				for(Object obj:selected){
					TUser user=(TUser) obj;
					names.append(user.getName()+"/");
				}
				this.info("您选择的是:"+names);
				target.add(feedback);
			}
		});
		
		//slider
		Label label=new Label("wk_slider_value", new Model<Integer>());
		form.add(label);
		
		final Slider slider=new Slider("wk_slider", new Model<Integer>(20),label);
		form.add(slider);
		form.add(new AjaxButton("wk_slider_save") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
				
				this.error("选择的数值为："+slider.getDefaultModelObject());
				
				target.add(feedback);
			}
		});
		
	}
}
